package com.zhujiaji.controller;

import com.zhujiaji.common.Result;
import com.zhujiaji.util.QiniuUtil;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author 谢金成
 * @date 2024/3/18 17:15
 */
@RestController
@RequestMapping("/api/qiniu/file")
public class UploadController {
    @PostMapping("/book/upload")
    public Result bookUpload(@RequestBody MultipartFile file, String imgPath) {
        if (!ObjectUtils.isEmpty(imgPath)) {
            QiniuUtil.delete(imgPath);
        }
        String url = QiniuUtil.upload(file);

        if (StringUtils.isEmpty(url)) {
            return Result.error("文件上传失败");
        }
        return Result.builder().code(200).message("文件上传成功").data(url).build();
    }
}
